package controllers;

import java.util.List;

import models.DemoEntry;
import models.Project;
import models.UserStory;
import play.mvc.*;

public class DemoEntries extends Controller{
    	/**
    	 * Method responsible for creating a new demo entry. It creates a new demo entry
    	 * using the Models.DemoEntry constructor
    	 * @param demo
    	 * 	The demo entry itself
    	 * @param type
    	 * 	String either Sucess or Failure 
    	 * @return
    	 * 	True or false based on the success of failure of the operation
    	 */
	public static boolean createDemoEntry(String demo, String type){
		DemoEntry temp = DemoEntry.find("demo", demo).first();
		if ( temp != null){		    
		    return false;
		}
	    	boolean flag;
		if ( type.equals("Success")){
			flag = true;
		}
		else{
			flag = false;
		}		
		DemoEntry newDemo = new DemoEntry(demo, flag).save();
		renderJSON( newDemo );
		return true;
	}
	/**
	 * Method responsible for associating a demo entry to a user story. Creates a new demo
	 * entry and associates it to a user story given the user story id.
	 * @param demo
	 * 	The description of the demo entry
	 * @param type
	 * 	Sucess or failure
	 * @param storiesId
	 * 	The id of the user story
	 * @return
	 * 	True of false based on the success of the operation
	 */
	public static boolean associateDemoEntry(String demo, String type, long storiesId){
	    	DemoEntry temp1 = DemoEntry.find("demo", demo).first();
	    	if ( temp1 != null){
		    //To indicate error
		    renderJSON ( temp1 ); 
		    return false;
		}	    	
		boolean flag;
		if ( type.equals("Sucess")){
			flag = true;
		}
		else {
			flag = false;
		}
		UserStory temp = UserStory.findById(storiesId);
		new DemoEntry (demo,flag,temp).save();
		return true;
	}
	/**
	 * Renders all user stories in a specific project.
	 * @param projectId
	 * 	The id of the project
	 */
	public static void index(long projectId){
		Project project = Project.findById(projectId);
		List < UserStory > userStories = UserStories.getAllUserStories(project);
		render (userStories);
	}
	/**
	 * Useless method
	 */
	
	public static boolean deleteEmptyEntries (){
		List <DemoEntry> allDemos = DemoEntry.all().fetch();
		for ( int i = 0 ; i < allDemos.size() ; i++ ){
			if ( allDemos.get(i).userStory == null ){
				allDemos.remove(i).save();
				i--;
			}
		}
		renderJSON ( allDemos );
		return true;
	}
	
	
	/**
	 * Deletes a demo entry given a specific demo entry id
	 * @param demoId
	 * 	Id of the demo entry to be deleted 
	 */
	public static boolean deleteDemoEntry(long demoId){
		DemoEntry.findById(demoId).delete();		
		return true;
	}
	/**
	 * Edits a demo entry by replacing old values by the
	 * new ones from the params. It then saves that object through
	 * Models.DemoEntry.
	 * @param demoId
	 * 	Id of the demo entry to be edited
	 * @param demo
	 * 	The new description of the demo enty
	 * @param type
	 * 	Sucess or Failure
	 * @return
	 * 	True or false based on the the success of the operation
	 */
	public static boolean editDemoEntry(long demoId, String demo, String type){
	    DemoEntry temp1 = DemoEntry.findById(demoId);
	    DemoEntry temp = DemoEntry.find("demo", demo).first();
		if ( temp != null && !temp1.demo.equals(temp.demo)){
		    System.out.println("return false");
		    return false;
		}
	    boolean flag;
		if ( type.equals("Sucess")){
			flag = true;
		}
		else {
			flag = false;
		}		

		DemoEntry.editDemoEntry(temp1, demo, flag);
		Object temp2 = new Object();
		renderJSON ( temp2 );
		return true;	    
	}
}
